package com.block.im.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.block.db.entity.ImUserBaseInfo;
import com.block.db.mapper.ImUserBaseInfoMapper;
import com.block.im.config.IMConfig;
import com.block.im.constant.UserInfoConstant;
import com.block.im.model.dto.IMHttpResultDTO;
import com.block.im.model.param.ImUserParam;
import com.block.im.service.IImBaseCommonService;
import com.block.im.service.IImUserBaseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.im.enums.UserTypeEnum;
import com.block.im.util.ImUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-24
 */
@Service
public class ImUserBaseInfoServiceImpl extends ServiceImpl<ImUserBaseInfoMapper, ImUserBaseInfo> implements IImUserBaseInfoService {

    @Autowired
    IImBaseCommonService imBaseCommonService;
    @Autowired
    IMConfig imConfig;

    @Override
    public ImUserBaseInfo getImUserInfo(ImUserParam imUserParam) {

        String userId = imUserParam.getUserId();
        ImUserBaseInfo userBaseInfo = this.getById(userId);

        boolean newFlag = ObjectUtil.isEmpty(userBaseInfo);
        if (ObjectUtil.isEmpty(userBaseInfo) ||
                !ObjectUtil.equals(Integer.valueOf(1), userBaseInfo.getStatus())){
            // 未注册成功，需要重新注册

            boolean b = this.importImUser(imUserParam);
            if (!b){
                return null;
            }

            userBaseInfo = new ImUserBaseInfo();
            userBaseInfo.setUserId(userId);
            userBaseInfo.setAccountId(imUserParam.getAccountId());
            userBaseInfo.setAccountType(imUserParam.getUserTypeEnum().getValue());

            userBaseInfo.setNickname(imUserParam.getNickname());
            userBaseInfo.setHeadUrl(imUserParam.getHeadImage());
            userBaseInfo.setStatus(1);

            // 系统管理员给 用户发送，只发送一次
            sendNoticeMsgFirst(imUserParam);

            boolean save = false;
            if (newFlag){
                save = this.save(userBaseInfo);
            }else{
               save = this.updateById(userBaseInfo);
            }
            if (!save){
                return null;
            }
        }
        return userBaseInfo;
    }

    /**
     * 导入 im 账号信息
     * @param imUserParam
     * @return
     */
    private boolean importImUser(ImUserParam imUserParam){

        if (StrUtil.isEmpty(imUserParam.getNickname())){
            imUserParam.setNickname(UserInfoConstant.getNickname());
        }
        if (StrUtil.isEmpty(imUserParam.getHeadImage())){
            imUserParam.setHeadImage(UserInfoConstant.getHeadImage());
        }

        List<ImUserParam> imUserParamList = new ArrayList<>();
        imUserParamList.add(imUserParam);

        IMHttpResultDTO imHttpResultDTO = imBaseCommonService.accountImport(imUserParamList);
        return imHttpResultDTO.isSuccess();
    }

    public void sendNoticeMsgFirst(ImUserParam toUser){

        String msgContent = "";
        if (StrUtil.equals(toUser.getUserTypeEnum().getValue(), UserTypeEnum.USER.getValue())){
            msgContent = "请注意防止诈骗，如果有诈骗行为，请举报。";
        }else if (StrUtil.equals(toUser.getUserTypeEnum().getValue(), UserTypeEnum.BUS.getValue())){
            msgContent = "请不要进行非法违法操作。否则会被封号处理。";
        }else{
            return;
        }

        ImUserParam fromUser = new ImUserParam(imConfig.getAdminAccountId(), UserTypeEnum.ROOT);
        imBaseCommonService.sendSingleMsgInfo(fromUser, toUser, msgContent);
    }

}
